﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace NetworkPong
{
    class Ball
    {
        public Vector2 Position
        {
            get { return position; }
            set { position = value; }
        }
        private Vector2 position;

        private Vector2 origin;

        private Texture2D texture;

        private Vector2 velocity;

        public bool IsActive = false;

        public Rectangle BB
        {
            get
            {
                return new Rectangle
                (
                    (int)(position.X - origin.X),
                    (int)(position.Y - origin.Y),
                    (int)(texture.Width),
                    (int)(texture.Height)
                );
            }
        }

        private int screenWidth;
        private int screenHeight;

        public Ball(ContentManager content, int screenWidth, int screenHeight)
        {
            this.texture = content.Load<Texture2D>("Ball");
            this.position = new Vector2(screenWidth / 2, screenHeight / 2);
            this.origin = new Vector2(texture.Width / 2, texture.Height / 2);

            Random rand = new Random();

            velocity = new Vector2(rand.Next(2, 5), rand.Next(1, 3));

            this.screenWidth = screenWidth;
            this.screenHeight = screenHeight;
        }

        public void Update()
        {
            if (IsActive)
                position += velocity;

            if (position.Y <= origin.Y || position.Y >= screenHeight - origin.Y)
            {
                velocity.Y *= -1;
            }

            if (position.X < 0 || position.X > screenWidth)
                Reset();
        }

        public void Reset()
        {
            Random rand = new Random();
            velocity = new Vector2(rand.Next(5, 8), rand.Next(3, 5));
            position = new Vector2(screenWidth / 2, screenHeight / 2);
            IsActive = false;
        }

        public void Flip()
        {
            velocity.X *= -1;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(texture, position, null, Color.White, 0.0f, origin, 1.0f, SpriteEffects.None, 1.0f);
        }
    }
}
